<template>
  <div class="manage">
    <a-button
      class="addBtn"
      style="margin-bottom: 24px"
      type="primary"
      @click="toManageUpdate('')"
      >新建模板</a-button
    >
    <div class="manage-list" v-if="templateList.length">
      <div v-for="item in templateList" :key="item.id" class="manage-card">
        <img src="@/assets/img/template.png" alt="" />
        <div class="manage-card-content">
          <div class="manage-card-content_top">
            <span class="manage-card-content_name">{{
              item.templateName
            }}</span>
            <span
              class="manage-card-content_btn"
              @click="toManageUpdate(item.id)"
              ><svg-icon icon-class="edit" /> 编辑</span
            >
            <span class="manage-card-content_btn" @click="handleCopy(item.id)"
              ><svg-icon icon-class="copy" /> 复制</span
            >
            <span class="manage-card-content_btn" @click="handleDelete(item.id)"
              ><svg-icon icon-class="delete" /> 删除</span
            >
          </div>
          <div class="manage-card-content_bottom">
            <span class="manage-card-content_date"
              >编辑时间: {{ item.updateTime }}</span
            >
            <div class="manage-card-content_group">
              <span
                :class="[
                  'manage-card-content_radio',
                  item.isDefault && 'active',
                ]"
                @click="handleChecked(item.id)"
              ></span>
              <span>默认模板</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-else class="empty">
      <img src="@/assets/img/empty.png" alt="" />
      <span>暂无数据</span>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { Modal, message } from "ant-design-vue";
import {
  salaryTemplatePage,
  deleteSalaryTemplate,
  setDefaultSalaryTemplate,
  copySalaryTemplate,
} from "@/service/modules/salary/salary";
const router = useRouter();
const templateList = ref([]);
// 新增/编辑模板
const toManageUpdate = (id: string) => {
  router.push({
    path: "/salary/manageUpdate",
    query: {
      id,
    },
  });
};
// 获取模板列表
const getSalaryTemplatePage = async () => {
  const { rows } = await salaryTemplatePage();
  templateList.value = [...rows];
};
// 删除模板
const handleDelete = (id) => {
  Modal.confirm({
    title: `是否确认删除此数据项`,
    content: "",
    onCancel: () => {},
    onOk: async () => {
      const { msg } = await deleteSalaryTemplate(id);
      message.success(msg || "操作成功！");
      getSalaryTemplatePage();
    },
  });
};
// 设置默认模版
const handleChecked = (id) => {
  Modal.confirm({
    title: `是否确认设置此模版为默认模版`,
    content: "",
    onCancel: () => {},
    onOk: async () => {
      const { msg } = await setDefaultSalaryTemplate(id);
      message.success(msg || "操作成功！");
      getSalaryTemplatePage();
    },
  });
};
// 复制模版
const handleCopy = async (id) => {
  const { msg } = await copySalaryTemplate(id);
  message.success(msg || "操作成功！");
  getSalaryTemplatePage();
};
onMounted(() => {
  getSalaryTemplatePage();
});
</script>
<style lang="less" scoped>
.campus-salary-web {
  cursor: pointer;
}
.manage {
  height: 100%;
  padding: 24px;
  background: #fff;
  .addBtn {
    border-radius: 6px;
    background: #3e77fa;
    border: 1px solid #3e77fa;
  }
  .empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top: 100px;
    img {
      width: 88px;
      height: 88px;
    }
    span {
      margin-top: 34px;
      font-size: 14px;
      line-height: 16px;
      letter-spacing: 1px;
    }
  }
  &-list {
    display: flex;
    flex-wrap: wrap;
  }
  &-card {
    display: flex;
    align-items: center;
    width: calc((100% - 24px) / 2);
    margin: 0 24px 24px 0;
    padding: 31px 24px;
    border: 1px solid #f0f1f7;
    border-radius: 10px;
    &:nth-of-type(2n) {
      margin-right: 0;
    }
    &-content {
      margin-left: 16px;
      flex: 1;
      &_top {
        display: flex;
        margin-bottom: 14px;
      }
      &_name {
        flex: 1;
        overflow: hidden;
        font-weight: 600;
        color: #303233;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      &_btn {
        margin-right: 16px;
        font-size: 14px;
        color: #3e77fa;
        cursor: pointer;
      }
      &_bottom {
        display: flex;
      }
      &_date {
        flex: 1;
        font-size: 14px;
        color: #aaaeb2;
      }
      &_group {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: #616466;
      }
      &_radio {
        position: relative;
        display: inline-block;
        width: 16px;
        height: 16px;
        margin-right: 8px;
        border: 1px solid #929599;
        border-radius: 50%;
        cursor: pointer;
        &.active {
          border-color: #3e77fa;
        }
        &.active::after {
          content: "";
          position: absolute;
          left: 3px;
          top: 3px;
          width: 8px;
          height: 8px;
          background-color: #3e77fa;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>
